<template>
	<view class="content">
		<scroll-view scroll-y class="left-aside">
			<view v-for="item in flist" :key="item.ID" class="f-item b-b" :class="{active: item.ID === currentId}"
				@click="tabtap(item)">
				{{item.CateName}}
			</view>
			<view style="height: 100rpx;"></view>
		</scroll-view>
		<scroll-view scroll-with-animation scroll-y class="right-aside" @scroll="asideScroll"
			:scroll-top="tabScrollTop">
			<view v-for="item in slist" :key="item.ID" class="s-list" :id="'main-'+item.ID" @click="toDoQues(item.ID)">
				<text class="s-item">{{item.CateName}}</text>
			</view>
			<view style="height: 100rpx;"></view>
		</scroll-view>
		<tabbar />
	</view>
</template>

<script>
	import tabbar from "@/components/tabbar"
	import {
		getCateGoryListAny
	} from "@/utils/api/category.js"
	export default {
		components: {
			tabbar,
		},
		data() {
			return {
				sizeCalcState: false,
				tabScrollTop: 0,
				currentId: 1,
				flist: [],
				slist: [],
				list: []
			}
		},
		onLoad(option) {
			if (!option.id) {
				this.currentId = 1
			} else {
				this.currentId = parseInt(option.id) // 对应一级分类id 
			}
			this.loadData();
		},
		mounted() {
			this.getCateList()
		},
		methods: {
			getCateList() {
				getCateGoryListAny({
					level: 1
				}).then(res => {
					if (res.data.code == 0) {
						this.flist = res.data.data
					}
				})
			},

			loadData() {
				// 载入对应二级分类
				getCateGoryListAny({
					level: 2,
					id: this.currentId
				}).then(res => {
					if (res.data.code == 0) {
						this.slist = res.data.data
					}
				})
			},

			//一级分类点击
			tabtap(item) {
				if (!this.sizeCalcState) {
					this.calcSize();
				}
				this.currentId = item.ID;
				this.loadData()
			},
			//右侧栏滚动
			asideScroll(e) {
				if (!this.sizeCalcState) {
					this.calcSize();
				}
				let scrollTop = e.detail.scrollTop;
				let tabs = this.slist.filter(item => item.top <= scrollTop).reverse();
				if (tabs.length > 0) {
					this.currentId = tabs[0].pid;
				}
			},
			//计算右侧栏每个tab的高度等信息
			calcSize() {
				let h = 0;
				this.slist.forEach(item => {
					let view = uni.createSelectorQuery().select("#main-" + item.ID);
					view.fields({
						size: true
					}, data => {
						item.top = h;
						h += data.height;
						item.bottom = h;
					}).exec();
				})
				this.sizeCalcState = true;
			},
			toDoQues(id) {
				uni.navigateTo({
					url: "/pages/Interview/Interview?id=" + id
				})
			},
			navToList(sid, tid) {
				uni.navigateTo({
					url: `/pages/brush/index?sid=${sid}&tid=${tid}`
				})
			}
		}
	}
</script>

<style lang='scss'>
	page,
	.content {
		height: 100%;
		background-color: #f8f8f8;
	}

	.content {
		display: flex;
	}

	.left-aside {
		flex-shrink: 0;
		width: 200upx;
		height: 100%;
		background-color: #f8f8f8;
	}

	.f-item {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100upx;
		font-size: 28upx;
		color: #333;
		position: relative;

		&.active {
			color: $themeColor;
			background: #fff;

			&:before {
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				height: 36upx;
				width: 8upx;
				background-color: #f2f2f2;
				border-radius: 0 4px 4px 0;
				opacity: .8;
			}
		}
	}

	.right-aside {
		flex: 1;
		overflow: hidden;
		padding-left: 20upx;
		background: #fff;
	}

	.s-item {
		display: flex;
		align-items: center;
		height: 70upx;
		padding-top: 8upx;
		font-size: 28upx;
		color: #333;
	}

	.t-list {
		width: 100%;
		background: #fff;
		padding-top: 12upx;

		&:after {
			content: '';
			flex: 99;
			height: 0;
		}
	}

	.t-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 26upx;
		color: #555;
		background: #f8f8f8;
		padding: 20upx;
		border-radius: 4rpx;
		margin-bottom: 10rpx;

		.iconfont {
			color: #999;
		}
	}
</style>